package org.ruoyi.aibox.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.ruoyi.common.idempotent.annotation.RepeatSubmit;
import org.ruoyi.common.log.annotation.Log;
import org.ruoyi.common.web.core.BaseController;
import org.ruoyi.core.page.PageQuery;
import org.ruoyi.common.core.domain.R;
import org.ruoyi.common.core.validate.AddGroup;
import org.ruoyi.common.core.validate.EditGroup;
import org.ruoyi.common.log.enums.BusinessType;
import org.ruoyi.common.excel.utils.ExcelUtil;
import org.ruoyi.aibox.domain.vo.AuditTaskVo;
import org.ruoyi.aibox.domain.bo.AuditTaskBo;
import org.ruoyi.aibox.service.IAuditTaskService;
import org.ruoyi.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 审核任务
 *
 * @author xpeter
 * @date 2025-04-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/aibox/auditTask")
public class AuditTaskController extends BaseController {

    private final IAuditTaskService auditTaskService;

    /**
     * 查询审核任务列表
     */
    @SaCheckPermission("aibox:auditTask:list")
    @GetMapping("/list")
    public TableDataInfo<AuditTaskVo> list(AuditTaskBo bo, PageQuery pageQuery) {
        return auditTaskService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出审核任务列表
     */
    @SaCheckPermission("aibox:auditTask:export")
    @Log(title = "审核任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AuditTaskBo bo, HttpServletResponse response) {
        List<AuditTaskVo> list = auditTaskService.queryList(bo);
        ExcelUtil.exportExcel(list, "审核任务", AuditTaskVo.class, response);
    }

    /**
     * 获取审核任务详细信息
     *
     * @param taskId 主键
     */
    @SaCheckPermission("aibox:auditTask:query")
    @GetMapping("/{taskId}")
    public R<AuditTaskVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long taskId) {
        return R.ok(auditTaskService.queryById(taskId));
    }

    /**
     * 新增审核任务
     */
    @SaCheckPermission("aibox:auditTask:add")
    @Log(title = "审核任务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AuditTaskBo bo) {
        return toAjax(auditTaskService.insertByBo(bo));
    }

    /**
     * 修改审核任务
     */
    @SaCheckPermission("aibox:auditTask:edit")
    @Log(title = "审核任务", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AuditTaskBo bo) {
        return toAjax(auditTaskService.updateByBo(bo));
    }

    /**
     * 删除审核任务
     *
     * @param taskIds 主键串
     */
    @SaCheckPermission("aibox:auditTask:remove")
    @Log(title = "审核任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{taskIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] taskIds) {
        return toAjax(auditTaskService.deleteWithValidByIds(List.of(taskIds), true));
    }

    /**
     * 提交AI审核任务
     */
    @PostMapping("/submit")
    @Log(title = "提交AI审核任务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    public R<String> submitTask(@RequestParam("file") MultipartFile file,
                               @RequestParam("modelName") String modelName,
                               @RequestParam("auditItemIds") String[] auditItemIds) {
        return R.ok(auditTaskService.submitTask(file, modelName, auditItemIds));
    }
}
